<?php

namespace Wechat\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Api\Client\ApiClient;
use WechatTool\WechatTool;
class WechatController extends AbstractActionController{


    public function connAction(){

        $options = array(
            'token'=>'yoursister', //填写你设定的key
       );
        $weObj = new WechatTool($options);
        $weObj->valid();
        $this->_doWechatRequset($weObj);
    }

    /**
     * @param WechatTool $weObj
     */
    private  function _doWechatRequset(WechatTool $weObj){
        $type = $weObj->getRev()->getRevType();

        switch($type) {
            case WechatTool::MSGTYPE_TEXT:
                $this->responseText($weObj);
                break;
            case WechatTool::MSGTYPE_EVENT:
                break;
            case WechatTool::MSGTYPE_IMAGE:
                break;
            default:
                $weObj->text("default reply")->reply();
        }

    }

    /**
     * @param WechatTool $weObj
     */
    private function responseText(WechatTool $weObj){

        $msg=$weObj->getRev()->getRevData();
        $regUrl="";

        if($msg=="txl"){
            //开始连接api端
            $addressBooks=ApiClient::getAddressBook();
            $weObj->text($addressBooks)->reply();
        }else if($msg=="c"){
            $weObj->text($regUrl)->reply();
        }
    }

}